package com.bs.action;

import java.util.List;
import net.sf.json.JSONArray;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.bs.bean.Manager;
import com.bs.service.ManagerService;
@SuppressWarnings("serial")
@ParentPackage("struts-default")
@Namespace("/manager")
public class ManagerAction extends ActionBase {

	private Manager manager;
	
	private ManagerService managerService;

	private String newPassword;

	public Manager getManager() {
		return manager;
	}

	public void setManager(Manager manager) {
		this.manager = manager;
	}

	public ManagerService getManagerService() {
		return managerService;
	}

	public void setManagerService(ManagerService managerService) {
		this.managerService = managerService;
	}
	
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	@Action("save")
	public void save() throws Exception{
		try {
			this.managerService.save(manager);
			response.getWriter().write(this.successInfo("添加管理员账号成功"));
		} catch (Exception e) {
			response.getWriter().write(this.errorInfo("添加管理员失败，可能是帐号已存在"));
			e.printStackTrace();
		}
	}
	@Action("delete")
	public void batchDelete() throws Exception{
		try {
			String ids = (String) request.getParameter("ids");
			int rows = this.managerService.batchDelete(ids);
			if(rows>=0){
				response.getWriter().write(this.successInfo("删除管理员账号失败"));
			}
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	
	@Action("findAll")
	public void findAll() throws Exception{
		try {
			List<Manager> list = this.managerService.findAll();
			JSONArray hospitals = JSONArray.fromObject(list);
			response.getWriter().write(hospitals.toString());
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	@Action("updatePurview")
	public void updatePurview() throws Exception{
		try {
			this.managerService.update(manager);
			response.getWriter().write(this.successInfo("修改管理员权限成功"));
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	
	@Action("changePassword")
	public void changePwd() throws Exception{
		try {
			int rows = this.managerService.changePwd(manager,newPassword);
			if(rows == -1){
				response.getWriter().write(this.errorInfo("原密码输入错误"));
			}else if(rows == 0){
				response.getWriter().write(this.errorInfo("管理员账号不存在"));
			}else{
				response.getWriter().write(this.successInfo("密码修改成功"));
			}
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	@Action("login")
	public void login() throws Exception{
		try{
			Manager m = this.managerService.login(manager);
			if(m!=null){
				response.getWriter().write("{status: true, purview:"+m.getPurview()+"}");
			}else{
				response.getWriter().write(this.errorInfo("用户名或密码错误"));
			}
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}

}
